package msi.gama.display.java3d.input;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import msi.gama.display.java3d.renderer.J3DRenderer;

/**
 * minhvv, p18, IFI, TPE 01/2014 - 07/2014
 */

public class InputProcessing implements KeyEventDispatcher {

	public static void startListening() {
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(new InputProcessing());
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent e) {
		if (e.getID() == KeyEvent.KEY_PRESSED) {
			processKeyPressed(e);
		}
		// else if (e.getID() == KeyEvent.KEY_RELEASED) {
		// } else if (e.getID() == KeyEvent.KEY_TYPED) {
		// }
		return false;
	}

	public void processKeyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();

		float offsetX = 0.0f;
		float offsetY = 0.0f;

		switch (keyCode) {
		case KeyEvent.VK_LEFT:
			offsetX = -0.1f;
			break;
		case KeyEvent.VK_RIGHT:
			offsetX = 0.1f;
			break;
		case KeyEvent.VK_UP:
			offsetY = 0.1f;
			break;
		case KeyEvent.VK_DOWN:
			offsetY = -0.1f;
			break;
		case KeyEvent.VK_R:
			J3DRenderer.resetSceneRotate();
			break;
		case KeyEvent.VK_SPACE:
			break;
		default:
			break;
		}

		if (offsetX != 0.0f || offsetY != 0.0f) {
			J3DRenderer.moveScreen(offsetX, offsetY);
		}
	}
}
